package com.wcl.nullmusic.utils;

import android.graphics.Bitmap;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.android.volley.AuthFailureError;
import com.android.volley.NetworkResponse;
import com.android.volley.ParseError;
import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.HttpHeaderParser;
import com.android.volley.toolbox.ImageRequest;
import com.android.volley.toolbox.StringRequest;
import com.android.volley.toolbox.Volley;
import com.wcl.nullmusic.base.BaseAppcation;

import java.io.UnsupportedEncodingException;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by dllo on 16/1/15.
 */
public class VolleyUtils {
    private static RequestQueue queue;

    private VolleyUtils() {
        queue = getQueue();
    }

    private RequestQueue getQueue() {
        if (queue == null) {
            queue = Volley.newRequestQueue(BaseAppcation.getContext());
        }
        return queue;
    }

    public static VolleyUtils getInstance() {
        return SingletonHolder.instance;
    }

    private static final class SingletonHolder {
        private static final VolleyUtils instance = new VolleyUtils();
    }

    public static void StringRequest(String url, Response.Listener<String> listener, Object tag) {
        StringRequest request = new StringRequest(url, listener, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                error.printStackTrace();
            }
        }) {
            @Override
            public Map<String, String> getHeaders() throws AuthFailureError {
                // 添加请求头 在请求头中增加属性
                Map<String, String> map = new HashMap<String, String>();
                // 在请求头中添加apikey
                map.put("Accept-Encoding", "");
                return map;
            }

            @Override
            protected Response<String> parseNetworkResponse(
                    NetworkResponse response) {
                try {
                    String jsonString = new String(response.data, "UTF-8");
                    return Response.success(jsonString,
                            HttpHeaderParser.parseCacheHeaders(response));
                } catch (UnsupportedEncodingException e) {
                    return Response.error(new ParseError(e));
                } catch (Exception je) {
                    return Response.error(new ParseError(je));
                }
            }
        };
        addRequest(request, tag);
    }

    public static void imageRequest(String url, Response.Listener<Bitmap> listener, Object tag) {
        ImageRequest request = new ImageRequest(url, listener,0, 0, Bitmap.Config.ARGB_8888, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                error.printStackTrace();
            }
        });
        addRequest(request, tag);
    }

    public static <T> void addRequest(Request<T> request, Object tag) {
        request.setTag(tag);
        queue.add(request);
    }

    public static void removeRequest(Object tag) {
        queue.cancelAll(tag);
    }
}
